package top.infopub.mgr.sts.web;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.bpa.service.EffectSureService;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.sts.domain.StsEffectDomain;
import top.infopub.model.hr.OrgInfo;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


/**
 * 管理驾驶舱-创效统计
 * @author wangw
 * @version 2020年3月20日
 * @see EffectStsController
 * @since
 */
@Controller
@RequestMapping("/effectSts")
public class EffectStsController extends GenericController {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private EffectSureService effectSureService;

    /**
     * 列表页
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if (StringUtils.isEmpty(visibleOrgIds)) {
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", "1");
            where.put("orderCondition", " types asc ");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }
        else {
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "sts/lay-effectSts-list";
    }

    /**
     * 根据条件获取获取列表数据
     * @param proName 项目名称
     * @param year 统计年份
     * @param orgTypeAndId ${org.id}-${org.types}
     * @param paramPage
     * @return 
     * @throws ParseException 
     * @see
     */
    @RequestMapping("/queryDataByCondition")
    public @ResponseBody RspVo<StsEffectDomain> queryDataByCondition(String proName,String year,
                                                                     String orgTypeAndId,ParamPage paramPage)
        throws ParseException {
        if (StringUtils.isBlank(year)) {
            return RspVo.getFailureResponse(-1, "请选择统计年~");
        }
        if (StringUtils.isBlank(orgTypeAndId)) {
            return RspVo.getFailureResponse(-1, "请选择公司主体~");
        }
        Page<StsEffectDomain> page = new Page<StsEffectDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        String orgId = orgTypeAndId.split("-")[0];
        String type = orgTypeAndId.split("-")[1];
        page = effectSureService.selectStsByDomainWhere(page, type, orgId, proName, year);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
}
